import request from '@/api/request'
import qs from 'qs'

let toFormData = (v) => {
  return qs.stringify(v, { arrayFormat: 'repeat' })
}

export function post(url, params,  contentType="json") {
  return request({
    url: url,
    method: 'post',
    data: contentType == 'json' ? params : toFormData(params, contentType),
    headers: contentType == 'json' ? { 'Content-Type': 'application/json;charset=UTF-8' } : { 'Content-Type': 'application/x-www-form-urlencoded' }
  })
}

export function postFile(url, params,  contentType="json") {
  return request({
    url: url,
    method: 'postFile',
    data: contentType == 'json' ? params : toFormData(params, contentType),
    headers: contentType == 'json' ? { 'Content-Type': 'application/json;charset=UTF-8' } : { 'Content-Type': 'application/x-www-form-urlencoded' }
  })
}

export function postFrom(url, params,  contentType="json") {
  return request({
    url: url,
    method: 'postFrom',
    data: contentType == 'json' ? params : toFormData(params, contentType),
    headers: contentType == 'json' ? { 'Content-Type': 'application/json;charset=UTF-8' } : { 'Content-Type': 'application/x-www-form-urlencoded' }
  })
}

export function postActions(url, params,  contentType="postFrom") {
  return request({
    url: url,
    method: 'post',
    data: contentType == 'json' ? params : toFormData(params, contentType),
    headers: contentType == 'json' ? { 'Content-Type': 'application/json;charset=UTF-8' } : { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' }
  })
}
export function put(url, params,  contentType="json") {
  return request({
    url: url,
    method: 'put',
    data: contentType == 'json' ? params : toFormData(params, contentType),
    headers: contentType == 'json' ? { 'Content-Type': 'application/json;charset=UTF-8' } : { 'Content-Type': 'application/x-www-form-urlencoded' }
  })
}

export function putFrom(url, params,  contentType="json") {
  return request({
    url: url,
    method: 'putFrom',
    data: contentType == 'json' ? params : toFormData(params, contentType),
    headers: contentType == 'json' ? { 'Content-Type': 'application/json;charset=UTF-8' } : { 'Content-Type': 'application/x-www-form-urlencoded' }
  })
}

export function _delete(url, params,  contentType="json1") {
  return request({
    url: url,
    method: 'delete',
    data: contentType == 'json' ? params : toFormData(params, contentType),
    headers: contentType == 'json' ? { 'Content-Type': 'application/json;charset=UTF-8' } : { 'Content-Type': 'application/x-www-form-urlencoded' }
  })
}
export function _get(url, params) {
  let baseUrl
  params ? baseUrl = url + params : baseUrl = url
  return request({
    url: baseUrl,
    method: 'get'
  })
}
export function get(url, params) {
  let baseUrl
  params ? baseUrl = url + params : baseUrl = url
  return request({
    url: baseUrl,
    method: 'get'
  })
}
//get
export function getAction(url,parameter) {
  return request({
    url: url,
    method: 'get',
    params: parameter
  })
}